package com.securitytrace.client.userlistener;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import com.securitytrace.client.ManagerClient;
import com.securitytrace.dataModel.AddressComBoxModel;
import com.securitytrace.dataModel.RoleComBoxModel;
import com.securitytrace.dataModel.UserDataModel;
import com.securitytrace.init.Init;
import com.securitytrace.listener.AddUserButtonListener;
import com.securitytrace.listener.UpdateUserButtonListener;
import com.securitytrace.model.User;
import com.securitytrace.popupwindow.UserWindow;

public class UserActionListener {
	private ManagerClient managerClient;

	public void addUser() {
		UserWindow userWindow = null;
		userWindow = new UserWindow(managerClient, true);
		AddUserButtonListener listener = new AddUserButtonListener(userWindow);
		userWindow.setAddUserButtonListener(listener);

		RoleComBoxModel roleComBoxModel = new RoleComBoxModel();
		roleComBoxModel.addItems(Init.getRoleService().getList());
		userWindow.getUserRoleComboBox().setModel(roleComBoxModel);
		AddressComBoxModel addressComBoxModel = new AddressComBoxModel();
		addressComBoxModel.addItems(Init.getAddressService().getList());
		userWindow.getUserAddressComboBox().setModel(addressComBoxModel);
		
		userWindow.setVisible(true);
	}
	
	public void updateUser() {
		int row = managerClient.getUserPanel().getUserTable().getSelectedRow();
		if(row >= 0) {
			User user = managerClient.getUserModel().getRow(row);
			UserWindow userWindow = new UserWindow(managerClient, true);
	
			userWindow.getUserAccountTextField().setText(user.getAccount());
			userWindow.getUserPasswordTextField().setText(user.getPassword());
			UpdateUserButtonListener listener = new UpdateUserButtonListener(userWindow);
			listener.setUser(user);
			userWindow.setAddUserButtonListener(listener);
			RoleComBoxModel roleComBoxModel = new RoleComBoxModel();
			roleComBoxModel.addItems(Init.getRoleService().getList());
			roleComBoxModel.setSelectedItem(user.getRole().getName());
			
			userWindow.getUserRoleComboBox().setModel(roleComBoxModel);
			AddressComBoxModel addressComBoxModel = new AddressComBoxModel();
			addressComBoxModel.addItems(Init.getAddressService().getList());
			addressComBoxModel.setSelectedItem(user.getAddress().getAddressName());
			
			userWindow.getUserAccountTextField().setEditable(false);
			userWindow.getUserRoleComboBox().setEnabled(false);
			userWindow.getUserRoleComboBox().setEditable(true);
			userWindow.getUserAddressComboBox().setEnabled(false);
			userWindow.getUserAddressComboBox().setEditable(true);
			userWindow.getUserAddressComboBox().setModel(addressComBoxModel);
	
			userWindow.setVisible(true);
			if (managerClient.getResultUser() != null) {
				managerClient.getUserModel().setValueAtRow(managerClient.getResultUser(), row);
			}
		}
	}
	
	public void deleteUser() {
		
		int row = managerClient.getUserPanel().getUserTable().getSelectedRow();
		if(row >= 0) {
			UserDataModel userDataModel = managerClient.getUserModel();
			User user = userDataModel.getRow(row);
			
			int currentPage = userDataModel.getUserCurrentPage();
			int pageSize = managerClient.getPageSize();
			int totalPage = 1;
			
			try {
				Init.getUserService().deleteById(user.getId());
				
				List<User> users = new ArrayList<User>();
				users = Init.getUserService().getListByPage((currentPage - 1) * pageSize, pageSize);
				if((0 >= users.size() || null == users) && currentPage > 1) {
					users = Init.getUserService().getListByPage((currentPage - 2) * pageSize, pageSize);
					userDataModel.setUserCurrentPage(currentPage - 1);
				}
				long userNumber = Init.getUserService().getTotalCount();
				if((userNumber % pageSize) == 0) {
					totalPage = (int) (userNumber / pageSize);
				}else {
					totalPage = (int) (userNumber / pageSize) + 1;
				}
				userDataModel.setUserTotal(totalPage);
				userDataModel.setDataVector(users);
				managerClient.getUserPanel().getPageInfoLabel().setText(userDataModel.getUserPageInfo());
			} catch(Exception ex) {
				JOptionPane.showMessageDialog(null,"操作失败");
				return ;
			}
		}
	}
	
	public void homePageAction() {
		if (managerClient.getUserModel().getUserCurrentPage() > 1) {
			UserDataModel model = managerClient.getUserModel();
			model.setDataVector(Init.getUserService().getListByPage(0,
							managerClient.getPageSize()));
			model.setUserCurrentPage(1);
			managerClient.getUserPanel().getUserTable().setModel(model);
			managerClient.getUserPanel().getPageInfoLabel()
					.setText(model.getUserPageInfo());
		}
	}
	
	public void previousPageAction() {
		UserDataModel model = managerClient.getUserModel();
		int currentPage = model.getUserCurrentPage();
		if (currentPage > 1) {
			model.setUserCurrentPage(currentPage - 1);
			model.setDataVector(Init.getUserService().getListByPage(
					(currentPage - 2) * managerClient.getPageSize(), managerClient.getPageSize()));
			managerClient.getUserPanel().getUserTable().setModel(model);
			managerClient.getUserPanel().getPageInfoLabel()
					.setText(model.getUserPageInfo());
		}
	}
	
	public void nextPageAction() {
		UserDataModel model = managerClient.getUserModel();
		int currentPage = model.getUserCurrentPage();
		int totalPage = model.getUserTotal();
		if (currentPage < totalPage) {
			model.setUserCurrentPage(currentPage + 1);
			model.setDataVector(Init.getUserService().getListByPage(
					currentPage * managerClient.getPageSize(), managerClient.getPageSize()));
			managerClient.getUserPanel().getUserTable().setModel(model);
			managerClient.getUserPanel().getPageInfoLabel()
					.setText(model.getUserPageInfo());
		}
	}
	
	public void lastPageAction() {
		UserDataModel model = managerClient.getUserModel();
		int currentPage = model.getUserCurrentPage();
		int totalPage = model.getUserTotal();
		if (currentPage < totalPage) {
			model.setUserCurrentPage(totalPage);
			model.setDataVector(Init.getUserService().getListByPage(
					(totalPage - 1) * managerClient.getPageSize(),
					managerClient.getPageSize()));
			model.setUserCurrentPage(totalPage);
			managerClient.getUserPanel().getUserTable().setModel(model);
			managerClient.getUserPanel().getPageInfoLabel()
					.setText(model.getUserPageInfo());
		}
	}
	
	/**以下为get/set方法**/
	public ManagerClient getManagerClient() {
		return managerClient;
	}
	public void setManagerClient(ManagerClient managerClient) {
		this.managerClient = managerClient;
	}
}
